/************************************************************************
                                                                     
   Xceed Ultimate ListBox for Silverlight                                                                                                                                            
   Copyright (C) 2010 Xceed Software Inc.    
                                                                     
   This program is provided to you under the terms of the GNU General Public  
   License version 2 as published by the Free Software Foundation. 
        
   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY, without even the implied warranty of 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License along 
   with this program, if not, write to the Free Software Foundation, Inc., 
   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

   This program can be provided to you by Xceed Software Inc. under a
   proprietary commercial license agreement for use in non-Open Source
   projects. Visit Xceed.com to find the commercial edition of 
   Xceed Ultimate ListBox for Silverlight.                                    
                                                                      
 **********************************************************************/

using System;
using System.Net;
using System.Linq;
using System.Collections;
using System.Windows;

namespace Xceed.Silverlight.Data
{
#if DEBUG
  internal class DebugQueryableHandler : QueryableHandler
  {
    private static readonly Guid GetItemCountCallId = DataUtils.CreateCallId();

    public DebugQueryableHandler( IQueryable data )
      :base(data)
    {
      this.SetItemCountSource( ValueSource.ApproximateCalculation );

    }

    public override ValueSource ItemCountSource
    {
      get
      {
        return m_countSource;
      }
    }

    public override bool IsFullySynchronized
    {
      get
      {
        return m_isFullySynchronized;
      }
    }

    public void SetItemCountSource( ValueSource source )
    {
      if( m_countSource != source )
      {
        m_countSource = source;
        m_countManager = ( source == ValueSource.ApproximateCalculation )
          ? new ApproximateCountManager()
          : null;
      }
    }

    public void SetIsFullySynchronized( bool isFullySynchronized )
    {
      m_isFullySynchronized = isFullySynchronized;
    }

    internal override void AddItemCountHint( int value, bool isMaximumValue )
    {
      if( this.ItemCountSource == ValueSource.ApproximateCalculation )
      {
        //We simulate an approximative item count
        if( m_countManager.AddItemCountHint( value, isMaximumValue ) )
        {
          this.RaiseItemCountChanged();
        }
      }
    }

    public override IAsyncResult BeginGetItemCount( AsyncCallback userCallback, object asyncState )
    {
      int count = ( this.ItemCountSource == ValueSource.ApproximateCalculation )
        ? m_countManager.ApproximateCount
        : this.QueryableSource.Cast<object>().Count();

      AsyncResult asyncResult = new AsyncResult(DebugQueryableHandler.GetItemCountCallId, userCallback,asyncState);
      if( !m_isFullySynchronized )
      {
        asyncResult.StartAsync( new GetCountResult( count ) );
      }
      else
      {
        asyncResult.StartSync( new GetCountResult( count ) );
      }
      return asyncResult;
    }

    public override GetCountResult EndGetItemCount( IAsyncResult getCountAsyncResult )
    {
      getCountAsyncResult.ValidateCallEnd( DebugQueryableHandler.GetItemCountCallId );
      return getCountAsyncResult.GetCallResult<GetCountResult>();
    }

    private bool m_isFullySynchronized = true;
    private ValueSource m_countSource = ValueSource.Real;
    private ApproximateCountManager m_countManager;
  }
#endif
}
